package com.eqdd.yiqidian.ui.gongneng.kehu;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.yiqidian.LianxirenInfoActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.LianxirenInfoResultBean;
import com.eqdd.yiqidian.bean.rebuild.LianxirenListResultBean;
import com.eqdd.yiqidian.bean.rebuild.SaleChanceListResultBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditTimeBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.utils.SingleChooseUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ActionSheetDialog;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.HashMap;

public class SaleChanceInfoActivity extends SlimBaseMultiEditActivity {
    private LianxirenInfoActivityCustom dataBinding;
    private String saleschanceid;
    private boolean isUpdate = false;
    private SaleChanceListResultBean items;
    private String cusid;
    private String contacts;
    private String clientName;
    private ActionSheetDialog chooseDialogRemind;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_lianxiren_info);
        initTopTitleBar(View.VISIBLE, "销售机会");
        isSave = false;
        isRead = false;
    }


    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (slimAdapterEx.getData().get(p).getPosition()) {
            case 3:
                Bundle bundle = new Bundle();
                bundle.putString("cusid", cusid);
                startActivityForResult(ChooseLianxirenActivity.class, bundle, Config.CHOOSE_LIANXIREN);
                break;
            case 1:
                if (chooseDialogRemind == null) {
                    SingleChooseUtil.getChooseDialog(SaleChanceInfoActivity.this, 37, (sheet) -> {
                        chooseDialogRemind = sheet;
                        sheet.show();
                    }, (position, strings) -> {
                        slimAdapterEx.getDataItem(p).setContent(strings.get(position - 1));
                        slimAdapterEx.notifyItemChanged(p);
                        update();
                    });
                } else {
                    chooseDialogRemind.show();
                }
                break;
        }
    }

    @Override
    protected void onTimeClick(int position) {
        super.onTimeClick(position);
        update();
    }

    @Override
    public void setView() {
        cusid = getIntent().getStringExtra("cusid");
        clientName = getIntent().getStringExtra(Config.CLIENT_NAME);

        items = (SaleChanceListResultBean) getIntent().getSerializableExtra("bean");
        saleschanceid = items.getId() + "";
        contacts = items.getContacts() + "";
        datas.add(new SlimEditEnterBean("机会名称", items.getChanceName(), 0, "请输入(必填)"));
        datas.add(new SlimEditEnterBean("机会分类", items.getChanceClassify() + "", 1,true, "请选择(必选)"));
        datas.add(new SlimEditHintBean("客户", clientName, 2));
        datas.add(new SlimEditEnterBean("联系人", items.getContactsName(), 3, true, "请选择(必选)"));
        datas.add(new SlimEditEnterBean("兴趣产品", items.getInterestproducts(), 4, "请输入(必填)"));
        datas.add(new SlimEditTimeBean("预期成交日期", items.getExdateofcompletion(), 5, "请输入"));
        datas.add(new SlimEditEnterBean("预期金额（元）", items.getExpectmoney(), 6, "请输入").addSchemes(StaticScheme.Numeric()));
        datas.add(new SlimEditEnterBean("产品销售金额（元）", items.getProductsalesmoney(), 7, "请输入").addSchemes(StaticScheme.Numeric()));
        datas.add(new SlimEditEnterBean("备注", items.getRemark(), 8, "请填写"));
        datas.add(new SlimEditTimeBean("提醒", items.getRemindTime(), 9, "请选择").setType(Config.YEAR_MONTH_DAY_HOUR_MINITE_TYPE));
        datas.add(new SlimEditHintBean("创建时间", items.getCreateTime(), 10));
        slimAdapterEx.updateData(datas);
    }

    @Override
    protected void setEditBeans() {

    }


    @Override
    public void onClick(View v) {
        super.onClick(v);

    }

    public void update() {
        OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.KEHU_UPDATE_SALE_CHANGE)
                .params("owner", user.getGuid())
                .params("saleschanceid", saleschanceid)
                .params("data",formatParam())
//                .params("chanceName", datas.get(0).getContent())
//                .params("chanceClassify", datas.get(1).getContent())
//                .params("contacts", contacts)
//                .params("interestproducts", datas.get(4).getContent())
//                .params("exdateofcompletion", datas.get(5).getContent())
//                .params("productsalesmoney", datas.get(6).getContent())
//                .params("expectmoney", datas.get(7).getContent())
//                .params("remark", datas.get(8).getContent())
//                .params("remindTime", datas.get(9).getContent())
                .execute(new DialogCallBack<HttpResult>(SaleChanceInfoActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            isUpdate = true;
                        }
                    }
                });
    }
    private String formatParam() {
        HashMap<String, String> map = new HashMap<>();
        map.put("chanceName",datas.get(0).getContent());
        map.put("chanceClassify",datas.get(1).getContent());
        map.put("contacts",contacts);
        map.put("interestproducts",datas.get(4).getContent());
        map.put("exdateofcompletion",datas.get(5).getContent());
        map.put("productsalesmoney",datas.get(6).getContent());
        map.put("expectmoney",datas.get(7).getContent());
        map.put("remark",datas.get(8).getContent());
        map.put("remindTime",datas.get(9).getContent());
        return new Gson().toJson(map);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.INTENT_CONTENT) {
            String content = data.getStringExtra("content");
            datas.get(requestCode).setContent(content);
            slimAdapterEx.notifyItemChanged(requestCode);
            update();
        }
        if (resultCode == Config.SUCCESS) {
            switch (requestCode) {
                case Config.CHOOSE_LIANXIREN:
                    LianxirenListResultBean bean = (LianxirenListResultBean) data.getSerializableExtra("bean");
                    contacts = bean.getId() + "";
                    slimAdapterEx.getData().get(3).setContent(bean.getName());
                    slimAdapterEx.notifyItemChanged(3);
                    break;
            }
        }
    }

    @Override
    public void onBackPressed() {
        if (isUpdate) {
            Intent intent = new Intent();
            items.setChanceName(datas.get(0).getContent());
            items.setChanceClassify(setParam(1));
            items.setContactsName(datas.get(3).getContent());
            items.setContacts(Integer.parseInt(contacts));
            items.setInterestproducts(datas.get(4).getContent());
            items.setExdateofcompletion(datas.get(5).getContent());
            items.setExpectmoney(datas.get(6).getContent());
            items.setProductsalesmoney(datas.get(7).getContent());
            items.setRemark(datas.get(8).getContent());
            items.setRemindTime(datas.get(9).getContent());
            intent.putExtra("bean", items);
            setResult(Config.SUCCESS, intent);
        }
        super.onBackPressed();

    }
}
